package com.ymatou.iapi.productlist.testcase;

import java.util.Arrays;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productlist.service.GetProductListByLiveIdCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.LiveCallService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.utils.EnvSetup;

public class Ts_GetProductListByLiveId {
	private static String sellerName = EnvSetup.getData("productsellername");
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据直播Id,查询商品id list");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(new GetProductListByLiveIdCall().getOpurl());
	}
	
	
	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductListByLiveId() {
		Logger.start(true, "根据直播Id,查询商品id list");
		try{
			
			String productId1 = SellerProductCallService.addNewProductNotOnSale(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");
			String productId2 = SellerProductCallService.addNewProductNotOnSale(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");

			int liveId = LiveCallService.createLiveInProgress(sellerName,sellerId).getJSONObject("Data").getInt("ActivityId");
			
			SellerProductCallService.addProductInLive(sellerId, liveId, productId1);
			SellerProductCallService.addProductInLive(sellerId, liveId, productId2);
			
			JSONObject requestBean = GetProductListByLiveIdCall.GetProductListByLiveId(liveId);
			
			Logger.verifyEquals(200, requestBean.getInt("Code"), "响应报文-返回码");

			JSONArray productIdList = requestBean.getJSONObject("Data").getJSONArray("ProductIdList");
			Logger.verifyEquals(2, productIdList.length(), "响应报文-返回码");
			
			for (int i=0;i<productIdList.length();i++){
				Logger.verifyEquals(true, Arrays.asList(productId1,productId2).contains(productIdList.getString(i)), "响应报文-ProductIdList");
			}
			
			
			
			
			 
		}catch(Exception e){
			Logger.fail(e);
		}
	}
}
